import { fileURLToPath, URL } from 'node:url';

import { defineConfig, ConfigEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
import htmlConfig from 'vite-plugin-html-config';
import { viteExternalsPlugin } from 'vite-plugin-externals';

export default ({ command, mode }: ConfigEnv) => {
  console.log(command, mode, '===');

  const cesiumPrefix = '/lib/Cesium/';
  const zz3dname = 'zz3d';
  // if (mode == "development") {
  //   cesiumPrefix = './lib/CesiumUnminified/'
  //   zz3dname = 'zz3d-src'
  // }

  const plugins = [vue(), vueDevTools()];

  const externalConfig = viteExternalsPlugin({
    cesium: 'Cesium',
    buildDate: new Date().toString()
  });
  plugins.push(externalConfig);

  const htmlConfigs = htmlConfig({
    headScripts: [
      {
        src: cesiumPrefix + 'Cesium.js'
      },
      {
        src: `/lib/zz3d/${zz3dname}.js`
      },
      {
        src: `/lib/plugins/compatible/version.js`
      },
      {
        src: `/lib/plugins/class/GaodeRoute.js`
      }
    ],
    links: [
      {
        rel: 'stylesheet',
        href: cesiumPrefix + 'Widgets/widgets.css'
      },
      {
        rel: 'stylesheet',
        href: `/lib/zz3d/${zz3dname}.css`
      }
    ]
  });
  plugins.push(htmlConfigs);

  return defineConfig({
    root: './',
    base: './',
    build: {
      assetsDir: './'
      // minify: ['false'].includes(env.VITE_IS_MINIFY) ? false : true
    },
    plugins: plugins,
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    }
  });
};
